<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名称">
        <el-input v-model="form.userName"></el-input
      ></el-form-item>
      <el-form-item label="年龄">
        <el-input v-model="form.age"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-input v-model="form.status"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">更新</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { updateUser } from "@/api/user";
import { getUserById } from "@/api/user";

export default {
  name: "AdminUpdateUser",
  data() {
    return {
      form: {
        id: "",
        userName: "",
        age: "",
        status: "",
      },
      id: "",
      showPage: true,
    };
  },
  methods: {
    createFormData(id) {
      const cid = {
        id: id,
      };
      getUserById(cid).then((res) => {
        if (res.code === 0) {
          this.form.id = res.data.id;
          this.form.userName = res.data.userName;
          this.form.age = res.data.age;
          this.form.status = res.data.status;
        }
      });
    },
    onSubmit() {
      const update = {
        id: this.form.id,
        userName: this.form.userName,
        age: this.form.age,
        status: this.form.status,
      };

      updateUser(update).then((res) => {
        if (res.code === 0) {
          // alert("okok");
          this.$cookies.set("cookie_update", true);
        }
      });
      setTimeout(() => {
        window.close();
      }, 1000); // 等待3秒钟
    },
  },
  mounted() {
    this.createFormData(this.$route.query.id);
  },
};
</script>

<style scoped></style>
